# -*- coding: utf-8 -*-
# FileName:     common.py
# time:         23/5/9 009 下午 7:32
# Author:       Zhou Hang
# Description:  I don't want to write
from datetime import datetime

# DIR PATH
DEFAULT_RECORD_DIR = "./output/records"
RESOURCE_DIR = "./resources"

SCR_H = 64
SCR_W = 128
MODE_LIST = ['1', 'L', 'I', 'F', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr' ]
MAX16BIT = 32768
MIN16BIT = -32768

SAMPLE_RATE = 44100  # 一秒采集44100个样本
MAX8BIT = 128
CHUNK = 1024

MIDI_DEVICE_BUFFER_SIZE = 100

NORMALKEY_MAPPER = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "  # midi音符和按键的映射
NORMALKEY_RANGE = (45, 82)  # 4从110Hz到880Hz的37个音对应的MidiNum为45~81
NORMALKEY_MAPPER_LEN = len(NORMALKEY_MAPPER)

MIDIKEY_RANGE = (48, 109)  # midi键盘支持的MidiNum为48~108
MIDIKEY_LEN = MIDIKEY_RANGE[1] - MIDIKEY_RANGE[0]

MAXVOL = 127  # 最大音量

# MIDI EVENT NUM
NOTE_ON = 144
NOTE_OFF = 128
TIMBRE_CHANGE = 192
MOD = 176
RECORD = 1
EXIT = 85
MOD_ON = 127
MOD_OFF = 0


def gen_unique_name():
    """
    根据日期和时间生成一个不会重名的名字
    :return:
    """
    now = datetime.now()
    return f"{now.year}-{now.month}-{now.day}-{now.hour}-{now.minute}-{now.second}"


def float2sample(data):
    """
    将float类型的数据转化为short类型再转bytes类型, 目前只支持2byte字节精度
    :param data:
    :return:
    """
    samplewidth = 2
    if data < -1.0:
        data = -1.0
    elif data > 1.0:
        data = 1.0
    data *= MAX16BIT
    if data == 1:
        data = MAX16BIT - 1  # 32768 is not a short
    data = round(data)
    if data < 0:
        data += (1 << (samplewidth * 8))  # python默认无法将负数转化为bytes，只好使用这种方法
    return data.to_bytes(length=samplewidth, byteorder="little")


def main():
    print(gen_unique_name())


if __name__ == "__main__":
    main()
